/**
 * Copyright (C) 2010 Alfredo Morresi
 * 
 * This file is part of RainbowLibs project.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package it.rainbowbreeze.libs.logic;

import it.rainbowbreeze.libs.common.IRainbowLogFacility;
import it.rainbowbreeze.libs.common.RainbowResultOperation;
import android.content.Context;
import android.os.Handler;

import static it.rainbowbreeze.libs.common.RainbowContractHelper.*;

/**
 * Read the log related to the application. Could then used for email, report etc
 * 
 * @author Alfredo "Rainbowbreeze" Morresi
 */
public class RainbowPrepareLogToSendThread
	extends RainbowBaseBackgroundThread<RainbowResultOperation<String>, String>
{
    //---------- Private fields
	protected final IRainbowLogFacility mBaseLogFacility;
	protected final String mLogTagToSearch;
    protected final RainbowCrashReporter mBaseCrashReporter;
    
    


    //---------- Constructors
    public RainbowPrepareLogToSendThread(
            Context context,
            Handler handler,
            int handlerMessageWhat,
            IRainbowLogFacility logFacility,
            RainbowCrashReporter crashReporter,
            String logTagToSearch)
    {
        super(context, handler, handlerMessageWhat);
        mBaseLogFacility = checkNotNull(logFacility, "LogFacility");
        mBaseCrashReporter = checkNotNull(crashReporter, "CrashReporter");
        mLogTagToSearch = checkNotNullOrEmpty(logTagToSearch, "LogTag to search");
    }




    //---------- Public fields
    



    //---------- Public methods
    @Override
    public RainbowResultOperation<String> executeTask() {
        //collect all the log (normal log + crash report)
//        mResultOperation = LogFacility.getLogData(new String[]{ GlobalDef.LOG_TAG, "AndroidRuntime"});
        RainbowResultOperation<String> resLog = mBaseLogFacility.getLogData(new String[]{mLogTagToSearch});
        RainbowResultOperation<String> resCrash = mBaseCrashReporter.getPreviousCrashReports(getContext());

        RainbowResultOperation<String> result;
        
        //merge two results
        if (!resLog.hasErrors() && !resCrash.hasErrors()) {
            result = new RainbowResultOperation<String>(resCrash.getResult() + resLog.getResult());
        } else if (!resLog.hasErrors()) {
            result = resLog;
        } else if (!resCrash.hasErrors()) {
            result = resCrash;
        } else {
            result = new RainbowResultOperation<String>();
        }
        
        return result;
    }
    



    //---------- Private methods
}
